/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package uk.co.alt236.btlescan.adapter;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.bluetooth.ble.GattCharacteristic;
import ohos.bluetooth.ble.GattService;
import uk.co.alt236.bluetoothlelib.resolvers.GattAttributeResolver;
import uk.co.alt236.btlescan.ResourceTable;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 * ConnectItemProvider
 *
 * @author ljx
 * @since 2021-04-29
 */
public class ConnectItemProvider extends BaseItemProvider {
    private List<GattService> list;
    private AbilitySlice slice;

    /**
     * ConnectItemProvider
     *
     * @param list
     * @param slice
     */

    public ConnectItemProvider(List<GattService> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    /**
     * setData
     *
     * @param listData
     */
    public void setData(List<GattService> listData) {
        this.list = listData;
        notifyDataChanged();
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()) {
            return list.get(position);
        }
        return Optional.empty();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_connect, null, false);
        } else {
            cpt = convertComponent;
        }
        GattService service = list.get(position);
        Text name = (Text) cpt.findComponentById(ResourceTable.Id_tv_name);
        Text mac = (Text) cpt.findComponentById(ResourceTable.Id_tv_uuid);
        Image arrow = (Image) cpt.findComponentById(ResourceTable.Id_iv_arrow);
        ListContainer listContainer = (ListContainer) cpt.findComponentById(ResourceTable.Id_item_list);

        DirectionalLayout itemView = (DirectionalLayout) cpt.findComponentById(ResourceTable.Id_root_item_view);
        itemView.setClickedListener(v -> {
            if (listContainer.getVisibility() == Component.HIDE) {
                listContainer.setVisibility(Component.VISIBLE);
                arrow.setImageAndDecodeBounds(ResourceTable.Media_arrow2);
            } else {
                listContainer.setVisibility(Component.HIDE);
                arrow.setImageAndDecodeBounds(ResourceTable.Media_arrow);
            }
            notifyDataChanged();
        });
        UUID uuid = service.getUuid();
        String attributeName = GattAttributeResolver.getAttributeName(uuid.toString(), "Unknown Service");
        name.setText(attributeName);
        mac.setText(uuid.toString());

        List<GattCharacteristic> characteristics = service.getCharacteristics();
        ItemItemProvider itemProvider = new ItemItemProvider(characteristics);
        listContainer.setItemProvider(itemProvider);
        return cpt;
    }

    /**
     * ItemItemProvider
     *
     * @author ljx
     * @since 2021-04-14
     */
    public class ItemItemProvider extends BaseItemProvider {
        private List<GattCharacteristic> list;

        /**
         * ItemItemProvider
         *
         * @param list
         */
        public ItemItemProvider(List<GattCharacteristic> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public Object getItem(int position) {
            if (list != null && position >= 0 && position < list.size()) {
                return list.get(position);
            }
            return Optional.empty();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
            final Component cpt;
            if (convertComponent == null) {
                cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_item_connect, null, false);
            } else {
                cpt = convertComponent;
            }
            GattCharacteristic service = list.get(position);

            Text name = (Text) cpt.findComponentById(ResourceTable.Id_tv_name);
            Text mac = (Text) cpt.findComponentById(ResourceTable.Id_tv_uuid);
            UUID uuid = service.getUuid();
            String attributeName = GattAttributeResolver.getAttributeName(uuid.toString(), "Unknown characteristic");
            name.setText(attributeName);
            mac.setText(uuid.toString());
            return cpt;
        }
    }
}